import pandas as pd
from wordcloud import WordCloud
import matplotlib.pyplot as plt

# 读取Excel文件
input_file = 'your_output_file.xlsx'
df = pd.read_excel(input_file)

# 提取sub_title和main_title列的内容
text_data = df['sub_title'].astype(str).tolist() + df['main_title'].astype(str).tolist()

# 将所有文本合并成一个大的文本
text = ' '.join(text_data)

# 指定支持中文的字体路径
font_path = 'C:/Windows/Fonts/simhei.ttf'  # 请替换为你的字体文件路径

# 生成词云
wordcloud = WordCloud(width=800, height=400, background_color='white', font_path=font_path).generate(text)

# 显示词云图
plt.figure(figsize=(10, 5))
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis('off')
plt.title('Word Cloud of sub_title and main_title')
plt.show()

# 保存词云图
output_image = 'wordcloud.png'
wordcloud.to_file(output_image)
print(f"词云图已保存到 {output_image}")